home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue16 / misc / syslogtk < prev   
Encoding:
Tcl/Tk script  |  2002-08-14  |  4.9 KB  |  163 lines

  1. #!/usr/local/bin/wish
  2. #
  3. # syslogtk      a simple tcl/tk script which is used to continuously display
  4. #               a syslog logging file
  5. #
  6. # Author:       John M. Fisk <fiskjm@ctrvax.vanderbilt.edu>
  7. # Version:      $Id: syslogtk,v 1.1.1.1 2002/08/14 22:26:58 dan Exp $
  8.  
  9. #############################################################################
  10. #                             SET GLOBAL VARIABLES                          #
  11. #############################################################################
  12.  
  13. # path to initial log file
  14. set logFile /var/adm/syslog
  15.  
  16. # number of rows in Maximized and Minimized window
  17. set maxRows 25
  18. set minRows 5
  19.  
  20. # initial number of rows & cols and the number of lines to 'tail'
  21. set initRows $minRows
  22. set initCols 80
  23. set tailLines 200
  24.  
  25. # on occasion, tail seems to "hang" after pppd terminates; as a kludge, set
  26. # up "auto-update" which restarts the tail process every autoUpdateMIN minutes
  27. set autoUpdateMIN 20
  28.  
  29. #############################################################################
  30. #                            PROCEDURE DEFINITIONS                          #
  31. #############################################################################
  32.  
  33. proc CreateWindow { rows cols } {
  34. #
  35. # creates the primary set of widgets and displays the main window --
  36. # it accepts two arguments:  the no. of rows and cols of the text
  37. # widget in which the file is displayed
  38.  
  39.     global logFile maxRows minRows tailLines fileList
  40.    
  41.     # FRAMES
  42.     frame .mbar -relief raised -bd 2
  43.     frame .info -relief flat
  44.     frame .text -relief groove -bd 2
  45.     frame .btn  -relief sunken -bd 2
  46.     pack .mbar .info .text .btn \
  47.          -side top -fill x -padx 1m -pady 1m
  48.  
  49.     # MENUBAR
  50.     menubutton .mbar.file -text "File" -underline 0 -menu .mbar.file.menu
  51.     pack .mbar.file -side left
  52.  
  53.     # ADD FILES IN /var/adm TO MENU
  54.     # for this to work, the user must have read permission on the files in
  55.     # /var/adm
  56.     menu .mbar.file.menu -tearoff false
  57.     foreach f [lsort [glob /var/adm/*]] {
  58.         if {[file isfile $f] && [file readable $f]} {
  59.         .mbar.file.menu add command -label $f \
  60.             -command [list ViewFile $f] 
  61.         }
  62.     }
  63.     .mbar.file.menu add separator
  64.     .mbar.file.menu add command -label "Exit" -command { exit }
  65.     
  66.     tk_menuBar .mbar .mbar.file
  67.  
  68.     # FILENAME LABEL
  69.     label .info.lbl -text "System Log File: "
  70.     entry .info.ent -textvariable logFile -relief flat
  71.     pack  .info.lbl .info.ent -side left
  72.  
  73.     # TEXT AND SCROLLBAR WIDGETS
  74.     text .text.txt -relief raised -bd 2 -yscrollcommand ".text.sB set" \
  75.         -width $cols -height $rows -wrap word
  76.     scrollbar .text.sB -command ".text.txt yview"
  77.     pack .text.sB -side right -fill y
  78.     pack .text.txt -side left
  79.  
  80.     # OPERATION BUTTONS
  81.     button .btn.head -text "Goto Head" -command { .text.txt see 1.0 }
  82.     button .btn.tail -text "Goto End"  -command { .text.txt see end }
  83.     button .btn.incr -text "Maximize Window" -command {
  84.         .text.txt configure -height $maxRows
  85.         .text.txt see end
  86.     }
  87.     button .btn.decr -text "Minimize Window" -command {
  88.         .text.txt configure -height $minRows
  89.         update
  90.         .text.txt see end
  91.     }
  92.     button .btn.update -text "Update" -command {
  93.         KillTail
  94.         LoadFile $logFile $tailLines
  95.     }
  96.     button .btn.quit -text "QUIT" -command { 
  97.         KillTail
  98.         exit  
  99.     }
  100.     pack .btn.head .btn.tail .btn.incr .btn.decr .btn.update .btn.quit \
  101.          -side left -padx 1m -pady 2m -fill x -expand TRUE
  102. }
  103.  
  104. proc LoadFile { file lines } {
  105. #
  106. # uses ``tail'' to display the file and ``fileevent'' to continuously 
  107. # update the display.  it takes two arguments:  the path to file which 
  108. # is to be displayed and the number of lines to tail
  109.  
  110.     global logFD tailPID
  111.  
  112.     if [catch { open "|tail -n $lines -f $file" RDONLY } logFD] {
  113.         puts stderr $logFD
  114.     } else {
  115.         .text.txt delete 1.0 end
  116.         set tailPID [pid $logFD]
  117.         fileevent $logFD readable {
  118.             gets $logFD line
  119.             .text.txt insert end $line\n
  120.             .text.txt see end
  121.         }
  122.     }
  123. }
  124.  
  125. proc ViewFile { filename } {
  126. #
  127. # This loads another log file in /var/adm by killing the current tail 
  128. # process and then loading up the file specified by "filename"
  129.  
  130.     global logFile tailLines
  131.  
  132.     set logFile $filename
  133.     KillTail
  134.     LoadFile $filename $tailLines
  135. }
  136.  
  137. proc KillTail {} {
  138. #
  139. # Kills the tail process and closes the file
  140.  
  141.     global logFD tailPID
  142.  
  143.     catch {exec kill -SIGTERM $tailPID}
  144.     catch {close $logFD}
  145. }
  146.  
  147. proc AutoUpdate { intervalMIN } {
  148. #
  149. # This automatically updates the display every ``interval'' minutes
  150.  
  151.     .btn.update invoke
  152.     after [expr $intervalMIN * 60 * 1000 ] AutoUpdate $intervalMIN
  153. }
  154.  
  155. #############################################################################
  156. #                                 MAIN PROGRAM                              #
  157. #############################################################################
  158.  
  159. # create window, load the default logfile, and start auto-updating
  160. CreateWindow $initRows $initCols
  161. LoadFile $logFile $tailLines
  162. AutoUpdate $autoUpdateMIN
  163.